package com.yanqu.road.logic.cross;

import com.yanqu.road.entity.rank.cross.CrossUserDecoration;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.logic.pb.DecorationPb;
import com.yanqu.road.logic.sort.CrossMammonSort;
import com.yanqu.road.pb.decoration.DecorationProto;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

public class CrossGroupMammonListModel {

    private int rankNum = GameConfig.RANK_DEFAULT_MAX_COUNT;

    private CrossMammonSort crossMammonSort;

    private boolean isNeedSync = false;

    private LinkedList<CrossUserDecoration> rankList;

    private int mammonDecorationId;

    private int groupId;

    private DecorationProto.MammonListMsg.Builder rankListMsg;

    public CrossGroupMammonListModel(int groupId, int mammonDecorationId) {
        crossMammonSort = new CrossMammonSort();
        rankList = new LinkedList<>();
        rankListMsg = DecorationProto.MammonListMsg.newBuilder();
        this.groupId = groupId;
        this.mammonDecorationId = mammonDecorationId;
    }

    public int getGroupId() {
        return groupId;
    }

    public int getMammonDecorationId() {
        return mammonDecorationId;
    }

    public boolean isNeedSync() {
        return isNeedSync;
    }

    public void setNeedSync(boolean needSync) {
        isNeedSync = needSync;
    }

    public DecorationProto.MammonListMsg.Builder getRankListMsg() {
        return rankListMsg.clone();
    }

    public LinkedList<CrossUserDecoration> getRankList() {
        return rankList;
    }

    public void setRankList(LinkedList<CrossUserDecoration> userRankList) {
        synchronized (this) {
            rankList = new LinkedList<>(userRankList);
            checkListValid();        //校验一下数组
            rankList.sort(crossMammonSort);
            while (rankList.size() > rankNum){
                rankList.removeLast();
            }
            long nowTime = System.currentTimeMillis() / 1000;
            while (rankList.size() > 0 && nowTime > rankList.getLast().getEndTime()){
                rankList.removeLast();
            }
            initRankMsg();
        }
    }

    /**
     * 校验数组，去掉重复数据
     */
    private void checkListValid(){
        List<Integer> needRemoveList = new ArrayList<>();
        List<Long> hasUserIdList = new ArrayList<>();
        for (int i = 0; i < rankList.size(); i++) {
            CrossUserDecoration userRank = rankList.get(i);
            if(!hasUserIdList.contains(userRank.getUserId())){
                hasUserIdList.add(userRank.getUserId());
            }else {
                needRemoveList.add(i);
            }
        }
        if(needRemoveList.size() > 0){
            Collections.sort(needRemoveList);
            Collections.reverse(needRemoveList);
            for (int index : needRemoveList) {
                rankList.remove(index);
            }
        }
    }

    private void initRankMsg() {
        rankListMsg = DecorationPb.parseMammonListMsg(rankList);
    }

    /**
     * 添加到排行
     */
    public void rankChange(CrossUserDecoration crossUserDecoration) {
        boolean isChange = false;
        synchronized (this) {
            int size = rankList.size();
            if (!rankList.stream().anyMatch(predicate -> predicate.getUserId() == crossUserDecoration.getUserId() &&
                    predicate.getUserBaseInfo().getServerId() == crossUserDecoration.getUserBaseInfo().getServerId())) {
                if (size < rankNum) {
                    CrossUserDecoration rank = crossUserDecoration.clone();
                    rankList.add(rank);
                    isChange = true;
                } else if (rankList.getLast().getEndTime() < crossUserDecoration.getEndTime()) {
                    CrossUserDecoration rank = rankList.getLast();
                    rank.setUserId(crossUserDecoration.getUserId());
                    rank.setUserBaseInfo(crossUserDecoration.getUserBaseInfo());
                    rank.setEndTime(crossUserDecoration.getEndTime());
                    rank.setModifyTime(crossUserDecoration.getModifyTime());
                    isChange = true;
                }
            } else {
                for (CrossUserDecoration userDecoration : rankList) {
                    if (userDecoration.getUserId() == crossUserDecoration.getUserId() && userDecoration.getUserBaseInfo().getServerId() == crossUserDecoration.getUserBaseInfo().getServerId()) {
                        if (userDecoration.getEndTime() != crossUserDecoration.getEndTime()) {
                            isChange = true;
                        }
                        userDecoration.setUserBaseInfo(crossUserDecoration.getUserBaseInfo());
                        userDecoration.setEndTime(crossUserDecoration.getEndTime());
                        userDecoration.setModifyTime(crossUserDecoration.getModifyTime());
                    }
                }
            }
            if(isChange) {
                rankList.sort(crossMammonSort);
                long nowTime = System.currentTimeMillis() / 1000;
                while ((rankList.size() > 0) && (nowTime > rankList.getLast().getEndTime())){
                    rankList.removeLast();
                }
                initRankMsg();
                isNeedSync = true;
            }
        }
    }

    /**
     * 添加到排行
     */
    public void checkOverTime() {
        synchronized (this) {
            long nowTime = System.currentTimeMillis() / 1000;
            boolean isChange = false;
            while ((rankList.size()) > 0 && (nowTime > rankList.getLast().getEndTime())){
                rankList.removeLast();
                isChange = true;
            }
            if(isChange) {
                initRankMsg();
                isNeedSync = true;
            }
        }
    }

    public void setRankNum(int rankNum) {
        this.rankNum = rankNum;
    }
}
